package com.chenye.importData;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Auther:chenye
 * @Data:2018/7/13
 * @Description:
 */
public class ImportReviewData {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException, IOException {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/reviewData?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "123456";
        Connection connection = DriverManager.getConnection(url, username, password);
        Statement st=connection.createStatement();
        connection.setAutoCommit(false);
        //读取文件内容
        BufferedReader fileReader = new BufferedReader(new FileReader("F:\\chenye\\javaCode\\private\\learnDemo\\importReviewData\\src\\main\\resources\\java温习知识点.txt"));
        String str = null;
        StringBuilder sb = new StringBuilder();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        while((str = fileReader.readLine()) != null){
            if(str.length()>0){
                sb.append("insert into question(body,insert_time) values('");
                sb.append(str);
                sb.append("','");
                sb.append(strDate);
                sb.append("')");
                st.execute(sb.toString());
                System.err.println(sb.toString());
                sb.delete(0,sb.toString().length());
            }
        }
        connection.commit();
        //关闭资源
        st.close();
        connection.close();
    }
}
